package com.lsec.springbootinit;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;


import java.net.InetAddress;

@Slf4j
@SpringBootApplication
//@MapperScan("com.lsec.springbootinit.mapper")  // 扫描mapper目录, 不用写@Mapper
@EnableCaching          // 开启cache
@ServletComponentScan
@EnableAsync
public class SpringBootInitApplication {

    @SneakyThrows
    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(SpringBootInitApplication.class, args);
        Environment env = app.getEnvironment();

        String appName = env.getProperty("spring.application.name");
        String serverPort = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path", "");

        String localUrl = String.format("http://localhost:%s%s", serverPort, contextPath);
        String externalUrl = String.format("http://%s:%s%s", InetAddress.getLocalHost().getHostAddress(), serverPort, contextPath);
        String docUrl = String.format("http://localhost:%s%s/doc.html", serverPort, contextPath);

        log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\t{}\n\t" +
                        "External: \t{}\n\t" +
                        "Doc: \t\t{}\n" +
                        "----------------------------------------------------------",
                appName,
                localUrl,
                externalUrl,
                docUrl);
    }

}
